<?php

class homeActions extends sfActions {

    public function executeBridge(sfWebRequest $request){
        $p = $request->getParameter('p');
        $nav = $request->getParameter('nav','');

        $conf = ConfigurationPeer::doSelectOne(new Criteria());
        $number_of_pages = $conf->getNumberOfPages();

        if($nav==2 || $nav==3){
            //nav hacia la izquierda
            $p = $p-8;
            if($p<=1){
                $p = 2;
            }
        }elseif($nav==18 || $nav==19 || $nav==20 || $nav==21){
            //nav hacia la derecha
            $p = $p+8;
            if($p>$number_of_pages){
                $p = $number_of_pages;
            }
        }

        $this->redirect("@homepage?p=".$p);
    }

    public function executeIndex(sfWebRequest $request){
        $p = $request->getParameter('p');

        /* este snippet hace que siempre que venga una uri sin hash, autoforce el hash en caso venga con #algo */
        if(!$this->getUser()->hasAttribute('has_hash')){
            $this->getUser()->setAttribute('has_hash', true);
            if($p<=9){
                if($p==""){ $p = 1; }
                $this->redirect("@homepage?p=".$p."#".$p);
            }else{
                $this->redirect("@homepage?p=".$p."#11");
            }
        }
        $this->getUser()->getAttributeHolder()->remove('has_hash');

        $conf = ConfigurationPeer::doSelectOne(new Criteria());
        $number_of_pages = $conf->getNumberOfPages();

        $start = 2;
        $end = $number_of_pages;
        if($p%2==0){
            $rango_start = 8;
            $rango_end = 9;
        }else{
            $rango_start = 9;
            $rango_end = 10;
        }

        $start = $p-$rango_start;
        if($start<=1){
            $start = 2;
        }

        $end = $p+$rango_end;
        if($end>$number_of_pages){
            $end = $number_of_pages;
        }

        $pages = array();
        $images_name = "LIM2012_%s.jpg";

        for($i=$start;$i<=$end;$i++){
            $images_name_zero_filled = str_pad($i, 4, "0", STR_PAD_LEFT);
            $pages[] = array("page" => $i, "filename" => sprintf($images_name, $images_name_zero_filled));
        }

        $this->p = $p;
        $this->pages = $pages;

        $using_ie = $this->using_ie();
        $this->using_ie = $using_ie;
        $this->ie_version = $this->ie_version();

        if($using_ie){
            return $this->setTemplate("index_ie");
        }else{
            return $this->setTemplate("index");
        }

    }

    private function using_ie(){
        $u_agent = $_SERVER['HTTP_USER_AGENT'];
        $ub = False;
        if(preg_match('/MSIE/i',$u_agent))
        {
            $ub = True;
        }

        return $ub;
    }
    
    private function ie_version() {
        $match=preg_match('/MSIE ([0-9].[0-9])/',$_SERVER['HTTP_USER_AGENT'],$reg);
        if($match==0)
            return -1;
        else
            return floatval($reg[1]);
    }

    public function executeSearch(sfWebRequest $request){
        $advertiser = $request->getParameter('advertiser');
        $item = $request->getParameter('item');
        $yell_page = $request->getParameter('yell_page');

        $this->getUser()->setAttribute('f_advertiser', $advertiser);
        $this->getUser()->setAttribute('f_item', $item);
        $this->getUser()->setAttribute('f_yell_page', $yell_page);
        
        $this->getUser()->getAttributeHolder()->remove('pagination_page');
        
        if($advertiser=="" && $item==""){
            if($yell_page!=""){
                $conf = ConfigurationPeer::doSelectOne(new Criteria());
                $number_of_pages = $conf->getNumberOfPages();

                if($yell_page<=0){
                    return $this->redirect("@homepage?p=1");
                }elseif($yell_page>=$number_of_pages){
                    return $this->redirect("@homepage?p=".$number_of_pages);
                }else{
                    return $this->redirect("@homepage?p=".abs($yell_page));
                }
            }else{
                return $this->redirect("@homepage");
            }
        }
    }

    public function executeDetail(sfWebRequest $request){
        $r_page = $request->getParameter('page');//1
        $hdAction = $request->getParameter('hdAction');//prev
        //$page = $this->getUser()->getAttribute('page',0);/* por default si no recibe el parametro de pagina manda al home */
        $page = $r_page;
        /* init vars */
        $first_page = 1;
        $conf = ConfigurationPeer::doSelectOne(new Criteria());
        $last_page = ($conf->getNumberOfPages()-1);

        if($hdAction=="first"){
            $page = $first_page;
        }elseif($hdAction=="prev"){
            if($page==2){
                $page = ($page-1);
            }else{
                $page = ($page-2);
            }
        }elseif($hdAction=="next"){
            $page = ($page+2);
        }elseif($hdAction=="last"){
            $page = $last_page;
        }else{
            $page = $r_page;
        }

        /* si es negativo, muestra la caratula */
        if($page<0){ $page = 0; }
        /* si es una pagina superior a la ultima*/
        if($page>=$last_page){ $page = $last_page; }

        if($page==0){ $this->redirect('@homepage'); }

        /*
        if($page==1){// si es la primera página
            $this->setTemplate('detail_first_page');
            $this->p = $page;
        }elseif($page==$last_page){// si es la última página
            $this->setTemplate('detail_last_page');
            $this->p = $page;
        }else{
        */
            $this->setTemplate('detail_two_pages');
            $this->p = $page;
        //}
        $this->getUser()->setAttribute('page',$page);
        
        $this->ie_version = $this->ie_version();
    }

}